// pages/orderCenter/cancelApply.js
import requestUtil from '../../baseRequest.js'
import requestUrls from '../../requestUrl.js'
import { $Message } from '../../components/base/index'

let openid = wx.getStorageSync('openid')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId: openid,
    list: ['运输计划有变', '信息填写有误，重新下单', '不想下单了', '没司机接单', '其他'],
    result: [],
    ifOther: true,
    radio: '',
    cancelReason: '',
    showLoading: false,
  },
  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
      ifOther: name !== '4',
      cancelReason: name !== '4' ? '' : this.data.cancelReason,
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail
    });
  },
  changeReason(event) {
    this.setData({
      reason: event.detail.value,
    })
  },
  cancelApplyOrder(e) {
    let cancelReason = this.data.ifOther ? this.data.list[this.data.radio] : e.detail.value.cancelReason;
    if (!cancelReason) {
      this.showErrorMessage("请选择取消原因");
      return false;
    }
    this.setData({showLoading:true});
    const formData = {
      openId: this.data.openId,
      criteria: {
        publishSide: this.data.publishSide,
        truckCenterId: this.data.truckCenterId,
        orderCenterId:this.data.orderCenterId,
        cancelReason,
      }
    }
    requestUtil.postRequest(requestUrls.truckOrder.cancelApply, formData, (res) => {
      if (!res.hasError) {
        this.setData({showLoading:false});
        wx.showModal({
          title: '温馨提示',
          content: '操作成功',
          showCancel: false,
          success: res => {
            wx.switchTab({
              url: '/pages/homev2/index'
            })
          }
        })
      }
    })
  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { publishSide, truckCenterId,orderCenterId } = options;
    this.setData({
      publishSide,
      truckCenterId,
      orderCenterId,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})